package com.itheima.service.impl;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @PackageName: com.itheima.service.impl
 * @ClassName: PermissionServiceImpl
 * @Author: zhouyangyang
 * @Date: 2020/2/15 20:49
 * @Description:
 */
@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;
    /**
     *新增权限
     *@returns
     */
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    /**
     *分页查询
     *@returns
     */
    public PageResult findByCondition(QueryPageBean queryPageBean) {
        //使用分页助手查询
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //进行字符串拼接
        String queryString = queryPageBean.getQueryString();
        if (StringUtils.isNotEmpty(queryString)){
            queryString = "%"+queryString+"%";
        }
        Page<Permission> page = permissionDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

   /**
    *删除权限
    *@returns
    */
    public void delete(Integer id) {
        //首先判断该权限是否被引用
        Long count = permissionDao.findByPermissionId(id);
        if (count > 0) {
            throw new RuntimeException("当前检查项被引用，不能删除");
        }
        permissionDao.deleteById(id);
    }

    /**
     *根据id查找权限信息
     *@returns
     */
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    /**
     *编辑权限
     *@returns
     */
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }

    /**
     *查询所有权限
     *@returns
     */
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }
}
